<template>
    <div class="login">
        <div class="box">
            <div class="boxHeader">
                <img src="../assets/imgLogo.png" alt="">
                华数学院管理系统
            </div>
            <eform
                :formData="formData"
                ref="form"
            ></eform>
            <a-button type="primary" class="loginBtn" @click="login()">登录</a-button>
        </div>
    </div>
</template>
<script>
import {loginApi} from '@/request/api'
import eform from '@/components/common/form'
export default {
    name:"login",
    components:{eform}, 
    mounted(){
        localStorage.setItem('mtoken','')
        localStorage.setItem('role',undefined)
    },
    data(){
        const formData=[{
            placeholder:'用户名：',
            prop:"account",
            rules:[{required:true,message:'请输入用户名'}],
        },{
            placeholder:'密码：',
            prop:"password",
            rules:[{required:true,message:'请输入密码'}],
            type:"password"
        }]
        return{
            formData,
        }
    },
    methods:{
        login(){
            this.$refs.form.form.validateFields().then(res=>{
                this.getToken(loginApi,res,(data)=>{
                    if(data.code==200){
                        this.$message.success('登录成功')
                        localStorage.setItem('mtoken',data.data.token)
                        localStorage.setItem('role',data.data.role)
                        localStorage.setItem('routeAuth',data.data.routeAuth)
                        localStorage.setItem('name',data.data.name)
                        let path
                        if(!data.data.routeAuth){
                            path="/userSchool"
                            this.$router.push({path})
                            return
                        }
                        if(data.data.routeAuth.indexOf('1')!=-1){
                            path='/examineeManage'//1+x管理
                        }else if(data.data.routeAuth.indexOf('2')!=-1){
                            path="/course"//教学课堂
                        }else if(data.data.routeAuth.indexOf('3')!=-1){
                            path="/news"//信息发布
                        }else if(data.data.routeAuth.indexOf('4')!=-1){
                            path="/userSchool"
                        }else{
                            path="/userSchool"
                        }
                        this.$router.push({path})
                    }else{
                        this.$message.error('登录失败')
                    }
                })
            })
        }
    }
}
</script>
<style>
#app .login .has-success.has-feedback .ant-form-item-children-icon{
    display: none;
}
.login{
    height:100%;
}
.login .ant-col-15{
    width:100%
}
.login .box{
    height:400px;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
}
.login .loginBtn{
    width:375px;
}
.login .boxHeader{
    display:flex;
    align-items: center;
    font-size:20px;
    font-weight:bold;
    padding-left:45px;
    margin-bottom:20px;
}
.login img{
    height:46px;
    margin-right:20px;
}
</style>